package com.cskaoyan.config.shiro;

import com.cskaoyan.bean.param.FileParam;
import com.cskaoyan.bean.po.AdminPO;
import com.cskaoyan.bean.po.wxPo.MarketUser;
import com.cskaoyan.bean.po.wxPo.MarketUserExample;
import com.cskaoyan.mapper.wxMapper.MarketUserMapper;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.RoleService;
import com.cskaoyan.util.StringToArrayUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CustomRealm extends AuthorizingRealm {
    @Autowired
    AdminService adminService;

    @Autowired
    RoleService roleService;

    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    protected AuthorizationInfo  doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        if (principalCollection.getPrimaryPrincipal() instanceof AdminPO) {
            AdminPO adminPO = (AdminPO) principalCollection.getPrimaryPrincipal();
            List<String> permissions = getPermissionsByAdminId(adminPO);
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            simpleAuthorizationInfo.addStringPermissions(permissions);
            return simpleAuthorizationInfo;
        }
        return null;
    }

    private List<String> getPermissionsByAdminId(AdminPO adminPO) {
        String str = adminPO.getRoleIds();
        List<String> permissions = new ArrayList<>();
        List<Integer> roleIds = StringToArrayUtils.toArray(str);
        for (Integer roleId : roleIds) {
            List<String> per = roleService.selectPermissionByRoleId(roleId);
            permissions.addAll(per);
        }
        System.out.println(permissions);
        return permissions;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MallToken mallToken = (MallToken) authenticationToken;
        String type = mallToken.getType();
        if ("admin".equals(type)) {
            List<AdminPO> adminPOs = adminService.selectAdminByAdminName(mallToken.getUsername());
            if (adminPOs != null && adminPOs.size() == 1) {
                AdminPO adminPO = adminPOs.get(0);
                String credentials = adminPO.getPassword();
                return new SimpleAuthenticationInfo(adminPO, credentials, this.getName());
            }
        } else {
            MarketUserExample marketUserExample = new MarketUserExample();
            marketUserExample.createCriteria().andUsernameEqualTo(mallToken.getUsername());
            List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
            if (marketUsers != null && marketUsers.size() == 1) {
                MarketUser marketUser = marketUsers.get(0);
                String credentials = marketUser.getPassword();
                return new SimpleAuthenticationInfo(marketUser, credentials, this.getName());
            }
        }
        return null;
    }
}
